<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class UserModel extends CI_Model {

    public function insertBySignup($username, $email, $password) {
        if ($username == '' || $email == '' || $password == '') {
            return false;
        }
        $data = array(
            'username' => $username,
            'email' => $email,
            'password' => md5($password),
            'createdate' => date("Y-m-d H:i:s")
        );
        return $this->db->insert('user', $data);
    }

    public function insert($data) {
        if (gettype($data) == "object") {
            return $this->db->insert('user', $data);
        } else {
            return false;
        }
    }

    public function getData() {
        $query = $this->db->get('user');
        return $query->result();
    }

    public function getDataByUserId($user_id) {
        $this->db->where('id', $user_id);
        $query = $this->db->get('user', 1, 0);
        log_message('info', get_class() . " -------- SQL : " . $this->db->last_query());
        return $query->row();
    }

    public function getNumRowsByUsernameAndPassword($username, $password) {
        $query = $this->db->query("SELECT * FROM user "
                . "WHERE (username = '" . $username . "' "
                . "OR EMAIL = '" . $username . "') "
                . "AND password = '" . md5($password) . "'");
        log_message('info', get_class() . " -------- SQL : " . $this->db->last_query());
        return $query;
    }

}
